package com.kedou.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kedou.app.R;
import com.kedou.domain.ProjectItem;
import com.kedou.widget.MyProgressBar;

public class ProlistListAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<ProjectItem> mProjects;

	public ProlistListAdapter(Context context, ArrayList<ProjectItem> projects) {
		this.mContext = context;
		this.mProjects = projects;
	}

	/**
	 * 上拉加载，下拉刷新 更新数据
	 * 
	 * @param mProjects
	 *            服务端返回的数据集合
	 * @param isRefresh
	 *            是否是下拉刷新
	 */
	public void addList(ArrayList<ProjectItem> mProjects, boolean isRefresh) {
		if (isRefresh) {
			this.mProjects.clear();
			this.mProjects.addAll(mProjects);
			this.notifyDataSetChanged();
		} else {
			this.mProjects.addAll(mProjects);
			this.notifyDataSetChanged();
		}
	}

	public ArrayList<ProjectItem> getList() {
		return this.mProjects;
	}

	@Override
	public int getCount() {
		return mProjects.size();
	}

	@Override
	public Object getItem(int position) {
		return mProjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ProjectItem projectItem = mProjects.get(position);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.project_item, null);
			holder = new ViewHolder();
			holder.tv_project_title = (TextView) convertView
					.findViewById(R.id.tv_project_title);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.img_project = (ImageView) convertView
					.findViewById(R.id.img_project);
			holder.tv_subtitle = (TextView) convertView
					.findViewById(R.id.tv_subtitle);
			holder.tv_percent_zc = (TextView) convertView
					.findViewById(R.id.tv_percent_zc);
			holder.progressBar1 = (MyProgressBar) convertView
					.findViewById(R.id.progressBar1);
			holder.tv_have_pay = (TextView) convertView
					.findViewById(R.id.tv_have_pay);
			holder.tv_goal = (TextView) convertView.findViewById(R.id.tv_goal);
			holder.tv_have_time = (TextView) convertView
					.findViewById(R.id.tv_have_time);
			holder.rl_project_detail = (RelativeLayout) convertView
					.findViewById(R.id.rl_project_detail);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_project_title.setText(projectItem.getProjectName());
		holder.tv_time.setText(projectItem.getCreateTime());
		// TODO 图片暂时不处理
		holder.tv_subtitle.setText(projectItem.getSubTitle());
		holder.tv_percent_zc.setText(projectItem.getProgress());
		holder.progressBar1.setProgress(Integer.valueOf(projectItem
				.getProgress().replace("%", "")));
		holder.tv_have_pay.setText(projectItem.getPaid());
		holder.tv_goal.setText(projectItem.getGoal());
		holder.tv_have_time.setText(projectItem.getEndTime());
		return convertView;
	}

	public class ViewHolder {
		/**
		 * 标题
		 */
		public TextView tv_project_title;
		/**
		 * 项目时间
		 */
		public TextView tv_time;
		/**
		 * 项目大图片
		 */
		public ImageView img_project;
		/**
		 * 项目二级标题
		 */
		public TextView tv_subtitle;
		/**
		 * 项目认筹比例
		 */
		public TextView tv_percent_zc;
		/**
		 * 项目认筹比例，进度条
		 */
		public MyProgressBar progressBar1;
		/**
		 * 已支付金额
		 */
		public TextView tv_have_pay;
		/**
		 * 众筹目标
		 */
		public TextView tv_goal;
		/**
		 * 剩余时间
		 */
		public TextView tv_have_time;
		/**
		 * 详细信息
		 */
		public RelativeLayout rl_project_detail;
	}
}
